package com.example.administrator.text.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

/**
 * Created by Administrator on 2016/7/22.
 * 获取手机或者app的相关信息
 */
public class getEquipmentData {
    private Context mContext;
    private static getEquipmentData mGetInformation;

    private getEquipmentData(Context context){
        this.mContext = context.getApplicationContext();
    }
    public static getEquipmentData newInstance(Context context){
        if(mGetInformation == null){
            synchronized (getEquipmentData.class){
                if(mGetInformation == null){
                    mGetInformation = new getEquipmentData(context);
                }
            }

        }
        return mGetInformation;
    }

    /**
     * 获取版本号
     */
    public String getVersion(){
        PackageManager manager = mContext.getPackageManager();
        PackageInfo info;
        try {
            info = manager.getPackageInfo(mContext.getPackageName(), 0);
            return info.versionName;
        } catch (PackageManager.NameNotFoundException e) {e.printStackTrace();}
        return "";
    }

    /**
     * 获取设备IP
     */
    public String getI_IP() {
        try {
            WifiManager wifimanage = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);//获取WifiManager
            //检查wifi是否开启
            if (!wifimanage.isWifiEnabled()) {
                return "";
            }
            WifiInfo wifiinfo = wifimanage.getConnectionInfo();
            String ip = intToIp(wifiinfo.getIpAddress());
            if(ip.equals("")||ip.equals("0.0.0.0")){
                return "";
            }else {
               return ip;
            }
        }catch (Exception e){
            return "";
        }
    }

    /**
     * 将获取的int转为真正的ip地址,参考的网上的，修改了下
     */
    private String intToIp(int i)  {
        return (i & 0xFF)+ "." + ((i >> 8 ) & 0xFF) + "." + ((i >> 16 ) & 0xFF) +"."+((i >> 24 ) & 0xFF );
    }

    /**
     *获取wifi 的mac   获取被连接网络的mac地址
     */
    public String getWiFiMac(){
        try {
            WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
            if(!wifi.isWifiEnabled()){
                return "";
            }
            WifiInfo info = wifi.getConnectionInfo();
            return info.getBSSID();
        }catch (Exception e){
            return "";
        }
    }

    /**
     * 获取设备 MAC(1)  获得本机的MAC地址
     */
    public String getIMAC(){
        try{
                WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
                WifiInfo info = wifi.getConnectionInfo();
                String getMac = info.getMacAddress();
                if( !getMac.equals("") && !getMac.equals("02:00:00:00:00:00") && !getMac.equals("00:00:00:00:00:00")){
                  return getMac;
                }else {
                    getMac = getProcessMAC();
                    if(getMac !=null &&!getMac.equals("") && !getMac.equals("02:00:00:00:00:00") && !getMac.equals("00:00:00:00:00:00")){
                       return getMac;
                    }else {
                        return "";
                    }
                }
        }catch (Exception e){
            return "";
        }

    }

    /**
     * 获取设备 MAC(2)
     */
    private String getProcessMAC(){
        String str = "";
        try {
            Process pp = Runtime.getRuntime().exec(
                    "cat /sys/class/net/wlan0/address ");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            for (; null != str;) {
                str = input.readLine();
                if (str != null) {
                    return str.trim();// 去空格获取设备MAC
                }
            }
        } catch (IOException ex) {
            // 赋予默认值
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * 获取wifi名称
     */
    public String getWIFIName(){
        WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifiMgr.getConnectionInfo();
        if(info != null){
            String wifiId = info.getSSID();
            String wifiName = wifiId.substring(0,wifiId.length());
            if(!wifiName.equals("") && wifiName.length() > 1){
                if(wifiName.substring(0,1).equals("\"")){
                    wifiName = wifiName.substring(1,wifiName.length()-1);
                    return wifiName;
                }else {
                    return wifiName;
                }
            }else {
                return "";
            }
        }else {
            return "";
        }
    }

}
